/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fatecpg.songs.intranet.managedbeans;

import br.com.fatecpg.songs.crud.facade.RegisterFacade;
import br.com.fatecpg.songs.dao.util.DAOException;
import br.com.fatecpg.songs.entity.City;
import br.com.fatecpg.songs.util.FacesContextUtil;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.hibernate.exception.ConstraintViolationException;

/**
 *
 * @author Marcos
 */
@ManagedBean
@ViewScoped
public class CityManagedBean implements RegisterManagedBean<City>{
    private int viewState;
    private RegisterFacade<City> facade = new RegisterFacade<City>(City.class);     
    private City city;    
    private List<City> list;

    public CityManagedBean() throws DAOException {        
        city = new City();              
        list = facade.list();
        viewState = LIST_STATE;
    }               
                      
    @Override
    public void openNew() {
        city = new City();
        viewState = REGISTER_STATE;
    }

    @Override
    public void openToUpdate() throws DAOException {               
        this.city = facade.load();
        this.viewState = REGISTER_STATE;
    }

    @Override
    public void register() throws DAOException{                
        facade.register(city);
        list = facade.list();
        viewState = LIST_STATE;
    }

    @Override
    public void delete() throws DAOException {                
        try{
            facade.delete();
            list = facade.list();
            this.viewState = LIST_STATE;            
        }catch(ConstraintViolationException ex){
             FacesContextUtil.addMessage("Existem músicos e/ou artistas desta cidade. Para deletá-la, é necessário deletar os músicos e os artistas.");
        }        
    }
    
    @Override    
    public List<City> getList() {        
        return list;
    }

    @Override
    public void cancel() {
        this.city = new City();
        this.viewState = LIST_STATE;
    }
    
    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }

    public int getViewState() {
        return viewState;
    }

    public void setViewState(int viewState) {
        this.viewState = viewState;
    }
}
